package com.yong.android.framework.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.RadioButton;

import com.yong.android.framework.R;


/**
 * 分为左/中/右 三个部分
 *
 * @author Veivei Peng
 */
public class BadgeRadioButton extends RadioButton {
    private static final String TAG = BadgeRadioButton.class.getSimpleName();
    private float mDrawableSizeWidth;
    private float mDrawableSizeHeight;
    private float mTextSize;
    private int mDrawablePadding;

    private static final int[] R_styleable_BadgeRadioButton = new int[]{
        /* 0 */ android.R.attr.textSize,
        /* 1 */ android.R.attr.drawablePadding,
            R.attr.drawableHeight,
            R.attr.drawableWidth,
    };

    public BadgeRadioButton(Context context) {
        this(context, null);
    }

    public BadgeRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (attrs != null) {
            TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R_styleable_BadgeRadioButton);
            mTextSize = typeArray.getDimension(0, -1.0f);
            mDrawablePadding = (int) typeArray.getDimension(1, -1.0f);
            mDrawableSizeHeight = typeArray.getDimension(2, -1.0f);
            mDrawableSizeWidth = typeArray.getDimension(3, -1.0f);
            typeArray.recycle();
        }

    }

    @Override
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        Drawable leftDrawable = getDrawable(left, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable topDrawable = getDrawable(top, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable rightDrawable = getDrawable(right, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable bottomDrawable = getDrawable(bottom, mDrawableSizeWidth, mDrawableSizeHeight);
        super.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
    }

    public void setCompoundDrawables(int left, int top, int right, int bottom) {
        Drawable leftDrawable = getDrawable(left, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable topDrawable = getDrawable(top, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable rightDrawable = getDrawable(right, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable bottomDrawable = getDrawable(bottom, mDrawableSizeWidth, mDrawableSizeHeight);
        super.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
    }

    private Drawable getDrawable(Drawable drawable, float width, float height) {
        if (drawable != null) {
            float drawableWidth = width >= 0 ? width : drawable.getMinimumWidth();
            float drawableHeight = height >= 0 ? height : drawable.getMinimumHeight();
            drawable.setBounds(0, 0, (int) drawableWidth, (int) drawableHeight);
        }
        return drawable;
    }

    private Drawable getDrawable(int drawableId, float width, float height) {
        Drawable drawable = null;
        if (drawableId > 0) {
            drawable = getDrawable(getResources().getDrawable(drawableId), width, height);
        }
        return drawable;
    }


    @Override
    public void setText(CharSequence text, BufferType type) {
        if (TextUtils.isEmpty(text)) {
            setTextSize(0f);
            setCompoundDrawablePadding(0);
        } else {
            setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
            setCompoundDrawablePadding(mDrawablePadding);
        }
        super.setText(text, type);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mIsBadgeVisible == true) {
            drawPoint(canvas);
        }
    }

    private void drawPoint(Canvas canvas) {
        Drawable leftCompDrawable = getCompoundDrawables()[1];
        if (leftCompDrawable != null) {
            int width = getWidth(); // 控件的宽度
            int height = getHeight();// 控件的高 度

            int pointCx;
            int pointCy;

            int radius = dip2px(getContext(), 4);

            Paint p = new Paint();
            p.setColor(Color.RED);// 设置红色
            p.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除，大家一看效果就明白了
            Rect rect = leftCompDrawable.getBounds();

            if ((gravity & Gravity.RIGHT) != 0) {
                pointCx = width / 2 + rect.right /2;
                pointCy = getPaddingTop() + radius/2;
                canvas.drawCircle(pointCx, pointCy, radius, p);// 小圆

            } else if ((gravity & Gravity.LEFT) != 0) {

            }

        }


    }


    /**
     * TODO: 对齐方式没有完成
     */
    private int gravity = Gravity.RIGHT | Gravity.TOP;
    private boolean mIsBadgeVisible = false;


    public void showPoint() {
        mIsBadgeVisible = true;
        invalidate();
    }


    public void hidePoint() {
        mIsBadgeVisible = false;
        invalidate();
    }


    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    @Override
    public void setChecked(boolean checked) {
        boolean ifInterruptChecked = false; // 判断是否中断check button的操作
        if (mInterruptCheckedListener != null && mInterruptCheckedListener.onInterruptChecked(this, checked)) {
            ifInterruptChecked = true;
        }

        if (!ifInterruptChecked) {
            super.setChecked(checked);
        }
    }

    public OnInterruptCheckedListener mInterruptCheckedListener;

    public interface OnInterruptCheckedListener {
        /**
         * @param view
         * @return ture check事件被截获；false 不拦截checked事件
         */
        boolean onInterruptChecked(View view, boolean checked);
    }

    public void setOnInterruptCheckedListener(OnInterruptCheckedListener listener) {
        mInterruptCheckedListener = listener;
    }


}
